local skel = fk.CreateSkill {
  name = "lb__shuyi",
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable {
  ["lb__shuyi"] = "书忆",
  [":lb__shuyi"] = "每轮开始时，你可以声明一个未因此声明过的牌名。本轮每阶段限一次，当有角色获得本轮声明的牌后，其可以重铸之，然后你可以令其摸两张牌并复原武将牌。",

  ["#lb__shuyi"] = "书忆：你可以声明一个未因此声明过的牌名",
  ["#lb__shuyi_recast"] = "书忆：你可以重铸这些牌",
  ["#lb__shuyi_draw"] = "书忆：你可以令 %dest 摸两张牌并复原武将牌",

  ["@lb__shuyi-round"] = "书忆",
  ["@[player]lb__shuyi"] = "书忆",

  ["$lb__shuyi1"] = "还未到止步的时候。",
  ["$lb__shuyi2"] = "旅程，不会结束。",
}

skel:addEffect(fk.RoundStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player:getMark("lb__sutu") == 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local all = Fk:getAllCardNames("btde", true)
    local names = table.simpleClone(all)
    for _, name in ipairs(all) do
      if table.contains(player:getTableMark(skel.name), name) then
        table.removeOne(names, name)
      end
    end
    if #names == 0 then return end
    local name = U.askForChooseCardNames(room, player, names, 1, 1, skel.name, "#lb__shuyi", all, true)
    if #name > 0 then
      event:setCostData(self, {name = name[1]})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local name = event:getCostData(self).name
    player.room:addTableMarkIfNeed(player, skel.name, name)
    player.room:setPlayerMark(player, "@lb__shuyi-round", name)
  end,
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(skel.name, true) and player:getMark("lb__sutu") ~= 0
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:setPlayerMark(player, "@lb__shuyi-round", "slash")
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryPhase) < 1
    and (player:getMark("@lb__shuyi-round") ~= 0 or player:getMark("lb__sutu") ~= 0) then
      local name = player:getMark("lb__sutu") == 0 and player:getMark("@lb__shuyi-round") or "slash"
      for _, move in ipairs(data) do
        if move.to and move.toArea == Card.PlayerHand then
          local cards = {}
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).trueName == name 
            and table.contains(move.to:getCardIds("he"), info.cardId) then
              table.insert(cards, info.cardId)
            end
          end
          if #cards > 0 then
            event:setCostData(self, {cards = cards, tos = {move.to}})
            return true
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    return 
        player.room:askToViewCardsAndChoice(event:getCostData(self).tos[1], {
          cards = event:getCostData(self).cards,
          skill_name = skel.name,
          choices = { "OK", "Cancel" },
          prompt = "#lb__shuyi_recast",
        }) == "OK"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    local to = event:getCostData(self).tos[1]
    cards = table.filter(to:getCardIds("he"), function (id)
      return table.contains(cards, id)
    end)
    if #cards > 0 then
      room:recastCard(cards, to, skel.name)
      if not to.dead and not player.dead and room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#lb__shuyi_draw::".. to.id}) then
        to:drawCards(2, skel.name)
        to:reset()
        if to ~= player then
          room:addTableMarkIfNeed(player, "@[player]lb__shuyi", to.id)
        end
      end
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, skel.name, 0)
  player.room:setPlayerMark(player, "@lb__shuyi-round", 0)
end)

return skel